package com.masc.gurr.model.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.masc.gurr.model.Contact;
import com.masc.gurr.model.Group;
import com.masc.gurr.model.ProtocolType;
import com.masc.gurr.model.SessionInfo;

public class Writer {

	public void writeContactList(String pStrUin, ProtocolType pProtocolType, Group pGroupRoot) {
		if (pProtocolType == ProtocolType.ICQ) {
			writeContactListICQ(pStrUin, pGroupRoot);
		}
	}
	
	public void writeInfo(SessionInfo pSessionInfo) {
		if (pSessionInfo == null)
			return;
		
		Element root = new Element("info");
		Document doc = new Document(root);

		root.setAttribute("uin", pSessionInfo.getUin());
		root.setAttribute("protocol", pSessionInfo.getProtocolType().name());
		root.setAttribute("counter", String.valueOf(pSessionInfo.getCounter()));
		
		Iterator<String> it = pSessionInfo.getMapUinSid().keySet().iterator();
		while (it.hasNext()) {
			String rec = it.next();
			Element lastsession = new Element("lastsession");
			lastsession.setAttribute("sid", String.valueOf(pSessionInfo.getMapUinSid().get(rec)));
			lastsession.setAttribute("recipient", rec);
			root.addContent(lastsession);
		}
		
		try {
			FileOutputStream out = new FileOutputStream("myinfo.xml");
			XMLOutputter serializer = new XMLOutputter();
			serializer.output(doc, out);
			out.flush();
			out.close();
		}
		catch (IOException e) {
			System.err.println(e);
		}
		
	}
	
	private void writeContactListICQ(String pStrUin, Group pGroupRoot) {
		if (pGroupRoot == null)
			return;
		
		Element root = new Element("contacts");
		Document doc = new Document(root);
		root.setAttribute("uin", pStrUin);
		root.setAttribute("protocol", "ICQ");
		parseContactListICQ(pGroupRoot, root);
		
		try {
			FileOutputStream out = new FileOutputStream("mycontacts.xml");
			XMLOutputter serializer = new XMLOutputter();
			serializer.output(doc, out);
			out.flush();
			out.close();
		}
		catch (IOException e) {
			System.err.println(e);
		}
	}

	private void parseContactListICQ(Group pGroupCurrent, Element pElementCurrent) {
		System.out.println("found grp " + pGroupCurrent.getName());
		List<Contact> cl = pGroupCurrent.getContacts();
		Iterator<Contact> itc = cl.iterator();
		while (itc.hasNext()) {
			Contact c = itc.next();
			System.out.println(" -> " + c.getName() + " (" + pGroupCurrent.getName() + ")");
			Element contact = new Element("contact");
			contact.setAttribute("uin", c.getUid());
			contact.setAttribute("name", c.getName());
			contact.setAttribute("statusmsg", c.getStatusMsg());
			pElementCurrent.addContent(contact);
		}
		
		List<Group> gl = pGroupCurrent.getSubgroups();
		Iterator<Group> itg = gl.iterator();
		while (itg.hasNext()) {
			Group g = itg.next();
			Element group = new Element("group");
			group.setAttribute("name", g.getName());
			pElementCurrent.addContent(group);
			parseContactListICQ(g, group);
		}
	}
	
}
